package com.cmict.data.system.api.user;

import com.cmict.data.common.websocket.distribute.MessageDO;
import com.cmict.data.framework.common.enums.CommonStatusEnum;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.datapermission.core.annotation.DataPermission;
import com.cmict.data.system.api.message.WsMessageDTO;
import com.cmict.data.system.api.user.dto.AdminUserRespDTO;
import com.cmict.data.system.convert.user.UserConvert;
import com.cmict.data.system.enums.ApiConstants;
import com.cmict.data.system.service.permission.PermissionService;
import com.cmict.data.system.service.user.AdminUserService;
import com.cmict.data.system.dal.dataobject.user.AdminUserDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = ApiConstants.VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
@Slf4j
@DataPermission(enable = false) //忽略数据权限
public class AdminUserApiImpl implements AdminUserApi {

    @Resource
    private AdminUserService userService;

    @Resource
    private PermissionService permissionService;

    @Override
    public CommonResult<AdminUserRespDTO> getUser(Long id) {
        AdminUserDO user = userService.getUser(id);
        AdminUserRespDTO adminUserRespDTO = UserConvert.INSTANCE.convert4(user);
        if(adminUserRespDTO != null){
            adminUserRespDTO.setRoleIds(permissionService.getUserRoleIdListByUserId(id));
        }
        return success(adminUserRespDTO);
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUsersByIdsJoin(String ids) {
        List<AdminUserDO> users = userService.getUsers(Arrays.stream(ids.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList()));
        return success(UserConvert.INSTANCE.convertList4(users));
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUsers(Collection<Long> ids) {
        List<AdminUserDO> users = userService.getUsers(ids);
        return success(UserConvert.INSTANCE.convertList4(users));
    }
    @Override
    public CommonResult<List<AdminUserRespDTO>> getUsersByDeptIdsJoin(String deptIds) {
        List<AdminUserDO> users = userService.getUsersByDeptIds(Arrays.stream(deptIds.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList()));
        return success(UserConvert.INSTANCE.convertList4(users));
    }
    @Override
    public CommonResult<List<AdminUserRespDTO>> getUsersByDeptIds(Collection<Long> deptIds) {
        List<AdminUserDO> users = userService.getUsersByDeptIds(deptIds);
        return success(UserConvert.INSTANCE.convertList4(users));
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUsersByPostIdsJoin(String postIds) {
        List<AdminUserDO> users = userService.getUsersByPostIds(Arrays.stream(postIds.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList()));
        return success(UserConvert.INSTANCE.convertList4(users));
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUsersByPostIds(Collection<Long> postIds) {
        List<AdminUserDO> users = userService.getUsersByPostIds(postIds);
        return success(UserConvert.INSTANCE.convertList4(users));
    }

    @Override
    public CommonResult<Boolean> validUsers(Set<Long> ids) {
        userService.validUsers(ids);
        return success(true);
    }

    @Override
    public List<AdminUserRespDTO> getAllUsers() {
        List<AdminUserDO> users = userService.getUsersByStatus(CommonStatusEnum.ENABLE.getStatus());
        return UserConvert.INSTANCE.convertList4(users);
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUsersByRoleIdsJoin(String roleIds) {
        List<AdminUserDO> users = userService.getUsersByRoleIds(Arrays.stream(roleIds.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList()));
        List<AdminUserRespDTO> adminUserRespDTOS = UserConvert.INSTANCE.convertList4(users);
        return success(adminUserRespDTOS);
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUsersByRoleIds(Collection<Long> roleIds) {
        List<AdminUserDO> users = userService.getUsersByRoleIds(roleIds);
        List<AdminUserRespDTO> adminUserRespDTOS = UserConvert.INSTANCE.convertList4(users);
        return success(adminUserRespDTOS);
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUsersByUserNamesInner(String names) {
        List<AdminUserDO> list = null;
        if(StringUtils.isBlank(names)){
             list = userService.selectListInner(new ArrayList<>());
        }else{
             list = userService.selectListInner(Arrays.stream(names.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList()));
        }
        List<AdminUserRespDTO> adminUserRespDTOS = UserConvert.INSTANCE.convertList4(list);
        return success(adminUserRespDTOS);
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUsersByUserNames(Collection<String> names) {
        List<AdminUserDO> list = userService.selectList(names);
        List<AdminUserRespDTO> adminUserRespDTOS = UserConvert.INSTANCE.convertList4(list);
        return success(adminUserRespDTOS);
    }

    @Override
    public List<AdminUserRespDTO> selectListAll() {
        return UserConvert.INSTANCE.convertList4(userService.selectListAll());
    }

    @Override
    public CommonResult<Collection<Object>> getSessionKeys() {
        return success(userService.getSessionKeys());
    }

    @Override
    public void distribute(WsMessageDTO ms) {
        MessageDO messageDO = new MessageDO();
        messageDO.setMessageText(ms.getMessageText());
        messageDO.setSessionKeys(ms.getSessionKeys());
        messageDO.setNeedBroadcast(ms.getNeedBroadcast());
        messageDO.setOnlyOneClientInSameKey(ms.getOnlyOneClientInSameKey());
        userService.distribute(messageDO);
    }

    @Override
    @DataPermission
    public CommonResult<List<String>> selectData2(String sql) {
        return success(userService.selectData2(sql));
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUsersByNickname(String nickname) {
        List<AdminUserDO> list = userService.getUsersByNickname(nickname);
        List<AdminUserRespDTO> adminUserRespDTOS = UserConvert.INSTANCE.convertList4(list);
        return success(adminUserRespDTOS);
    }

}
